package com.zggk.lzz.xcjl;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.zggk.lzz.R;
import com.zggk.lzz.adapter.LGRecycleViewAdapter;
import com.zggk.lzz.adapter.LGViewHolder;
import com.zggk.lzz.mvp.MVPBaseActivity;
import com.zggk.lzz.seach.SeachActivity;
import com.zggk.lzz.util.Utils;
import com.zggk.lzz.xcbg.XcbgActivity;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * MVPPlugin
 * 邮箱 784787081@qq.com
 */

public class XcjlActivity extends MVPBaseActivity<XcjlContract.View, XcjlPresenter> implements XcjlContract.View {

    @Bind(R.id.back)
    ImageView back;
    @Bind(R.id.title_text)
    TextView titleText;
    @Bind(R.id.cancle_button)
    TextView cancleButton;
    @Bind(R.id.sx_te)
    TextView sxTe;
    @Bind(R.id.xcjl_te)
    TextView xcjlTe;
    @Bind(R.id.re_lay)
    RecyclerView reLay;
    @Bind(R.id.disease_new_parent_layout)
    RelativeLayout diseaseNewParentLayout;
    private LGRecycleViewAdapter<XcjlBean.DataBean.RecordsBean> adapter;
    private ArrayList<XcjlBean.DataBean.RecordsBean> ListDatas = new ArrayList<>();
    private String orgName;
    private String userName;
    private String startDate;
    private String endDate;

    @Override
    public void onRequestError(String msg) {
    }

    @Override
    protected int getLayout() {
        return R.layout.act_xcjl;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
        initView();
        initData();
    }

    private void initData() {
        mPresenter.getData(orgName, userName, startDate, endDate,XcjlActivity.this);
    }

    private void initView() {
        setTitleText("巡查记录");
        goBack();
        reLay.setHasFixedSize(true);
        reLay.setNestedScrollingEnabled(false);
        LinearLayoutManager manager = new LinearLayoutManager(getContext());
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        reLay.setLayoutManager(manager);
        adapter = new LGRecycleViewAdapter<XcjlBean.DataBean.RecordsBean>(ListDatas) {
            @Override
            public int getLayoutId(int viewType) {
                return R.layout.item_xcjl;
            }
            @Override
            public void convert(LGViewHolder holder, XcjlBean.DataBean.RecordsBean zCDBean, int position) {
                holder.setText(R.id.name_te, zCDBean.getUserName());
                holder.setText(R.id.qm_te, Utils.replaceNull(zCDBean.getXclc()) + "km");
                holder.setText(R.id.dz_te, zCDBean.getOrgName());
                holder.setText(R.id.sj_te, getDateTimeFromMillisecond(zCDBean.getXcStartTime()));
            }
        };
        reLay.setAdapter(adapter);
        adapter.setOnItemClickListener(R.id.item_layout, new LGRecycleViewAdapter.ItemClickListener() {
            @Override
            public void onItemClicked(View view, int position) {
                Intent intent = new Intent(XcjlActivity.this, XcbgActivity.class);
                intent.putExtra("id", ListDatas.get(position).getId());
                startActivity(intent);
            }
        });
    }

    public static String getDateTimeFromMillisecond(String millisecond){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZZZ");
        Date date = new Date();
        try {
            date = sdf.parse(millisecond);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        SimpleDateFormat sdf2=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String sDate=sdf2.format(date);
        return sDate;
    }

    @OnClick({ R.id.sx_te})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.sx_te:
                Intent intent1 = new Intent(XcjlActivity.this, SeachActivity.class);
                startActivityForResult(intent1, 1);
                break;
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == 2) {
            if (requestCode == 1) {
                orgName = data.getStringExtra("ZQMC");
                userName = data.getStringExtra("LZXM");
                startDate = data.getStringExtra("QSSJ");
                endDate = data.getStringExtra("JSSJ");
                mPresenter.getData(orgName, userName, startDate, endDate,XcjlActivity.this);
            }
        }
    }

    @Override
    public void getDatas(XcjlBean.DataBean dataBean) {
        ListDatas.clear();
        ListDatas.addAll(dataBean.getRecords());
        adapter.notifyDataSetChanged();
        xcjlTe.setText("共上报" + ListDatas.size() + "个巡查记录");
    }

}
